﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using kwzx.Common;

namespace kwzx
{
    public partial class dbrw_xxbj : Form
    {
        DataSet1TableAdapters.T_USERTableAdapter tuser = new DataSet1TableAdapters.T_USERTableAdapter();
        DataSet1TableAdapters.T_USER2TableAdapter tuser2 = new DataSet1TableAdapters.T_USER2TableAdapter();
        DataSet1TableAdapters.K_kwzxrwTableAdapter kwzxrw = new DataSet1TableAdapters.K_kwzxrwTableAdapter();
        DataSet1TableAdapters.T_GROUPTableAdapter ds2group = new DataSet1TableAdapters.T_GROUPTableAdapter();
        DataSet1TableAdapters.X_CS_DETAILTableAdapter ds2detail = new DataSet1TableAdapters.X_CS_DETAILTableAdapter();

        int userid = 0;
        int user2id = 0;
        int kwzxrwid = 0;
        public dbrw_xxbj()
        {
            InitializeComponent();
        }

        public dbrw_xxbj(int userid1,int user2id1,int kwzxrwid1)
        {
            userid = userid1;
            user2id = user2id1;
            kwzxrwid = kwzxrwid1;
            InitializeComponent();
        }

        private void userbj_Load(object sender, EventArgs e)
        {
            this.t_USERTableAdapter.FillBy_ID(this.dataSet1.T_USER, userid);
            this.t_USER2TableAdapter.FillBy_user_id(this.dataSet1.T_USER2, user2id);

            //正式库表中对应的照片
            if (t_USERBindingSource.Current != null)
            {
                string users_id9 = ((DataRowView)t_USERBindingSource.Current)["user_id"].ToString();
                  
                string Imgpath = ((DataRowView)t_USERBindingSource.Current)["zp"].ToString(); ;
                if (!string.IsNullOrWhiteSpace(Imgpath))
                {
                    int x = Imgpath.IndexOf("ftp/") + 4;
                    string userImg = Imgpath.Substring(x, Imgpath.Length - x);   //ftp下载路径
                    int userImginfo = userImg.IndexOf("/") + 1;
                    string spath = SPicUtils.path + "\\";
                    string zpname = users_id9 + ".dat";
                    string kkzp = FileTran.DownloadFile(SPicUtils.path, zpname, userImg);
                    this.zPPictureBox.Image = SPicUtils.PicConvert(kkzp);
                }
                else
                {
                    zPPictureBox.Image = global::kwzx.Properties.Resources.无图片;
                }
            }

            //临时表中对应的照片
            if (t_USER2BindingSource.Current != null)
            {
                string users_id9 = ((DataRowView)t_USERBindingSource.Current)["user_id"].ToString();
                string Imgpath = ((DataRowView)t_USER2BindingSource.Current)["zp"].ToString(); ;
                if (!string.IsNullOrWhiteSpace(Imgpath))
                {
                    int x = Imgpath.IndexOf("ftp/") + 4;
                    string userImg = Imgpath.Substring(x, Imgpath.Length - x);   //ftp下载路径
                    int userImginfo = userImg.IndexOf("/") + 1;

                    string zpname = users_id9 + ".dat";
                    string kkzp = FileTran.DownloadFile(SPicUtils.path, zpname, userImg);
                    this.zpPictureBox1.Image = SPicUtils.PicConvert(kkzp);
                }
                else
                {
                    zpPictureBox1.Image = global::kwzx.Properties.Resources.无图片;
                }
            }
        }
        /// <summary>
        /// 更换用户信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            DataTable tuserdt = tuser.GetDataBy_ID(userid);
            DataTable tuser2dt = tuser2.GetDataBy_user_id(user2id);
           

            tuserdt.Rows[0]["DEPT_id"] = tuser2dt.Rows[0]["DEPT_id"];
            tuserdt.Rows[0]["ry_id"] = tuser2dt.Rows[0]["RY_ID"];
            tuserdt.Rows[0]["uby"] = tuser2dt.Rows[0]["gsbm_id"];
            tuserdt.Rows[0]["xm"] = tuser2dt.Rows[0]["xm"];
            tuserdt.Rows[0]["zsxm"] = tuser2dt.Rows[0]["zsxm"];
            
            tuserdt.Rows[0]["xb"] = tuser2dt.Rows[0]["xb"];
            tuserdt.Rows[0]["dh"] = tuser2dt.Rows[0]["dh"];
            tuserdt.Rows[0]["dh2"] = tuser2dt.Rows[0]["dh2"];
            tuserdt.Rows[0]["zw"] = tuser2dt.Rows[0]["zw"];
            tuserdt.Rows[0]["gz"] = tuser2dt.Rows[0]["gz"];
            tuserdt.Rows[0]["sfzh"] = tuser2dt.Rows[0]["sfzh"];
            tuserdt.Rows[0]["zc"] = tuser2dt.Rows[0]["zc"];
            //照片只修改了地址，并没有拷贝
            tuserdt.Rows[0]["zp"] = tuser2dt.Rows[0]["zp"];

            tuserdt.Rows[0]["byyx"] = tuser2dt.Rows[0]["byyx"];
            tuserdt.Rows[0]["bysj"] = tuser2dt.Rows[0]["byrq"];
            tuserdt.Rows[0]["xl"] = tuser2dt.Rows[0]["xl"];
            tuserdt.Rows[0]["rzrq"] = tuser2dt.Rows[0]["rzrq"];
            tuserdt.Rows[0]["tsgzzjhm"] = tuser2dt.Rows[0]["tsgzzjhm"];
            tuserdt.Rows[0]["tszjyxq"] = tuser2dt.Rows[0]["tsgzzjyxq"];
            tuserdt.Rows[0]["tszjfsrq"] = tuser2dt.Rows[0]["tsgzzjfsrq"];
            tuserdt.Rows[0]["bzrq"] = tuser2dt.Rows[0]["bzrq"];

            Utils.CommonData cd = new Utils.CommonData();
            //生成新的人员编号
            tuserdt.Rows[0]["rybh"] = cd.getRybh(tuser2dt.Rows[0]["groupname"].ToString(), int.Parse(tuser2dt.Rows[0]["gz"].ToString()));

            //用tuser2表中的记录更新到tuser表中
            tuser.Update(tuserdt.Rows[0]);

            string sbsm =((DataTable)kwzxrw.GetDataBy_kwzxrwid(kwzxrwid)).Rows[0]["sbsm"].ToString();
            kwzxrw.UpdateQuery_sbsm(sbsm+",数据更新完成", kwzxrwid); 

            MessageBox.Show("数据更换成功！");
            
            //数据重新加载
            userbj_Load(sender, e);

            this.DialogResult = DialogResult.OK;
            this.Close();
        }
        /// <summary>
        /// 不更换用户信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            //判断要是用户的姓名、所属单位、归属部门、工种有一项不一致时，提示必需要更新数据。
            DataTable tuserdt = tuser.GetDataBy_ID(userid);
            DataTable tuser2dt = tuser2.GetDataBy_user_id(user2id);
            string mess = "";
            if (tuserdt.Rows[0]["xm"].ToString() != tuser2dt.Rows[0]["xm"].ToString())//姓名
            {
                mess+="姓名不相同!";
            }
            if (tuserdt.Rows[0]["DEPT_id"].ToString() != tuser2dt.Rows[0]["DEPT_id"].ToString())//所属部门
            {
                mess += "所属部门不相同！";
            }
            if (tuserdt.Rows[0]["uby"].ToString() != tuser2dt.Rows[0]["uby"].ToString())//归属部门
            {
                mess += "归属部门不相同！";
            }
            if (tuserdt.Rows[0]["gz"].ToString() != tuser2dt.Rows[0]["gz"].ToString())//工种
            {
                mess+="工种不相同！";
            }

            if (mess.Length > 0)
            {
                MessageBoxButtons messButton = MessageBoxButtons.OKCancel;
                DialogResult dr = MessageBox.Show(mess + "必须替换信息!", "信息替换", messButton);
                if (dr == DialogResult.OK)
                {
                    //必须替换用户数据
                    string sbsm = ((DataTable)kwzxrw.GetDataBy_kwzxrwid(kwzxrwid)).Rows[0]["sbsm"].ToString();
                    kwzxrw.UpdateQuery_sbsm(sbsm + ",数据更新", kwzxrwid);

                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    //不替换用户数据
                    this.DialogResult = DialogResult.Cancel;
                    this.Close();
                    return;
                }
            }
            else
            {
                //不更换修改任务表状态
                string sbsm = ((DataTable)kwzxrw.GetDataBy_kwzxrwid(kwzxrwid)).Rows[0]["sbsm"].ToString();
                kwzxrw.UpdateQuery_sbsm(sbsm + ",数据不更新", kwzxrwid);

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }
    }
}
